// ListCtrlChecked.cpp : implementation file
//

#include "stdafx.h"
#include "ListCtrlChecked.h"

#include "ItemsArray.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CListCtrlChecked

CListCtrlChecked::CListCtrlChecked(const int iKeyColumnIndex /*=0*/)
	: m_iKeyCol(iKeyColumnIndex)
	, m_ifRecordStatesChange(RECORD_CHANGE)
{
}

CListCtrlChecked::~CListCtrlChecked()
{
}


BEGIN_MESSAGE_MAP(CListCtrlChecked, CListCtrlData)
	//{{AFX_MSG_MAP(CListCtrlChecked)
	ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListCtrlChecked message handlers

void CListCtrlChecked::PreSubclassWindow() 
{
	SetExtendedStyle( GetExtendedStyle() |LVS_EX_CHECKBOXES );

	CListCtrlData::PreSubclassWindow();
}

void CListCtrlChecked::SetCheckeds(const CString &strCheckedKeys, BOOL ifRecordStatesChange /*=RECORD_CHANGE*/)
{
	if (strCheckedKeys.IsEmpty())
		return;

	BOOL bBackupRecordState= m_ifRecordStatesChange;
	m_ifRecordStatesChange = ifRecordStatesChange;
	{
		CMap<CString,LPCTSTR,int,int&> mapItemsIndexs;
		const int iItemsCount= GetItemCount();
		for (int iItem= 0; iItem < iItemsCount; iItem++)
		{
			CString & strItemKey= GetItemText( iItem, m_iKeyCol );
			mapItemsIndexs[strItemKey] = iItem;
		}

		CStringItemsArray arrChecked(strCheckedKeys);
		for (int iChecked= 0; iChecked < arrChecked.GetSize(); iChecked++)
		{
			int iIndex= -1;
			if (TRUE == mapItemsIndexs.Lookup( arrChecked[iChecked], iIndex ))
				SetCheck( iIndex );
		}
	}
	m_ifRecordStatesChange = bBackupRecordState;
}

const CString CListCtrlChecked::GetCheckedKeys()
{
	CStringItemsArray arrRoles;
		POSITION pos= m_mapRecordedCheckedKeys.GetStartPosition();
		while (pos)
		{
			CString strRoleName;	CString strIdle;
			m_mapRecordedCheckedKeys.GetNextAssoc( pos, strRoleName, strIdle );
			arrRoles.Add( strRoleName );
		}
	return (arrRoles.GetSerialData());
}

void CListCtrlChecked::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = reinterpret_cast<NMLISTVIEW*>(pNMHDR);
	
	if (RECORD_CHANGE == m_ifRecordStatesChange)
	{
		if ((-1 != pNMListView->iItem)
		 && ((LVIS_STATEIMAGEMASK & pNMListView->uNewState) || (LVIS_STATEIMAGEMASK & pNMListView->uOldState)))
		{
			CString &strKey= GetItemText( pNMListView->iItem, m_iKeyCol );
			BOOL isChecked= ((LVIS_STATEIMAGEMASK & pNMListView->uNewState) >> 12) - 1;
				if (isChecked)
					m_mapRecordedCheckedKeys.SetAt( strKey, CString() );
				else
ASSERT(TRUE==		m_mapRecordedCheckedKeys.RemoveKey( strKey )	);
		}
	}

	*pResult = 0;
}

// void CAddUserGroupDlg::OnClickLstRoles(NMHDR* pNMHDR, LRESULT* pResult) 
// {
// 	LV_HITTESTINFO *pHitTestInfo= reinterpret_cast<LV_HITTESTINFO*>(pNMHDR);
// 	pHitTestInfo->flags = LVHT_ONITEMSTATEICON;
// 	int iItem= m_clstRoles.HitTest( pHitTestInfo );
// 
// 	if ((-1 != pHitTestInfo->iItem)	// Some item is clicked
// 	 && (LVHT_ONITEMSTATEICON & pHitTestInfo->flags))
// 	{
// 		const BOOL ifChecked= m_clstRoles.GetCheck( pHitTestInfo->iItem );
// 		m_clstRoles.SetCheck( pHitTestInfo->iItem, (TRUE == ifChecked)?FALSE:TRUE );
// 	}
// 
// 	*pResult = 0;
// }
